home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / orca / outline.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  7.4 KB  |  228 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Provides a simple utility to draw outlines on the screen.
  5. [[[TODO: WDW - The Wedge support is experimental and not perfect.]]]'''
  6. __id__ = '$Id: outline.py 4221 2008-09-15 08:11:23Z wwalker $'
  7. __version__ = '$Revision: 4221 $'
  8. __date__ = '$Date: 2008-09-15 04:11:23 -0400 (Mon, 15 Sep 2008) $'
  9. __copyright__ = 'Copyright (c) 2008 Sun Microsystems Inc.'
  10. __license__ = 'LGPL'
  11. import cairo
  12.  
  13. try:
  14.     import gtk
  15.     display = gtk.gdk.display_get_default()
  16.     screen = display.get_default_screen()
  17.     screen_width = screen.get_width()
  18.     screen_height = screen.get_height()
  19. except:
  20.     pass
  21.  
  22. import orca_state
  23. import settings
  24.  
  25. def _adjustToScreen(x, y, width, height):
  26.     if x < 0:
  27.         width = width + x
  28.         x = 0
  29.     elif x >= screen_width:
  30.         width -= x - screen_width
  31.         x = screen_width
  32.     
  33.     if y < 0:
  34.         height = height + y
  35.         y = 0
  36.     elif y >= screen_height:
  37.         height -= y - screen_height
  38.         y = screen_height
  39.     
  40.     if x + width >= screen_width:
  41.         width = screen_width - x
  42.     
  43.     if y + height >= screen_height:
  44.         height = max(1, screen_height - y)
  45.     
  46.     width = max(1, width)
  47.     height = max(1, height)
  48.     return [
  49.         x,
  50.         y,
  51.         width,
  52.         height]
  53.  
  54. _outlineWindows = []
  55.  
  56. class Line(gtk.Window):
  57.     '''Draws a simple filled box on the display using
  58.     settings.outlineColor for the color.
  59.  
  60.     Arguments:
  61.     - x, y, width, height: the dimensions of the box
  62.     '''
  63.     
  64.     def __init__(self, x, y, width, height):
  65.         gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
  66.         self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(settings.outlineColor[0], settings.outlineColor[1], settings.outlineColor[2]))
  67.         self.set_property('accept-focus', False)
  68.         self.set_decorated(False)
  69.         self.set_keep_above(True)
  70.         self.set_skip_taskbar_hint(True)
  71.         self.move(x, y)
  72.         self.set_default_size(width, height)
  73.         self.resize(width, height)
  74.         self.realize()
  75.  
  76.  
  77.  
  78. class Wedge(Line):
  79.     '''Draws a filled isosceles triangle on the display
  80.     using settings.outlineColor for the color.
  81.  
  82.     Arguments:
  83.     - x, y, width, height - the bounding box of the triangle
  84.     - top - if true, the triangle is flat on the bottom,
  85.             if false, the triangle is pointy on the bottom
  86.     '''
  87.     
  88.     def __init__(self, x, y, width, height, top):
  89.         self.top = top
  90.         Line.__init__(self, x, y, width, height)
  91.         self.connect('size-allocate', self._on_size_allocate)
  92.  
  93.     
  94.     def _on_size_allocate(self, win, allocation):
  95.         '''Sets the shape of the window to a triangle.'''
  96.         width = allocation.width
  97.         height = allocation.height
  98.         bitmap = gtk.gdk.Pixmap(None, width, height, 1)
  99.         cr = bitmap.cairo_create()
  100.         cr.set_source_rgb(0, 0, 0)
  101.         cr.set_operator(cairo.OPERATOR_DEST_OUT)
  102.         cr.paint()
  103.         cr.set_operator(cairo.OPERATOR_OVER)
  104.         if self.top:
  105.             cr.move_to(width / 2, 0)
  106.             cr.line_to(width / 2 + height / 2, height)
  107.             cr.line_to(width / 2 - height / 2, height)
  108.         else:
  109.             cr.move_to(width / 2, height)
  110.             cr.line_to(width / 2 + height / 2, 0)
  111.             cr.line_to(width / 2 - height / 2, 0)
  112.         cr.close_path()
  113.         cr.fill()
  114.         win.shape_combine_mask(bitmap, 0, 0)
  115.  
  116.  
  117.  
  118. class Box(Line):
  119.     '''Draws the outline of a rectangle on the display
  120.     using settings.outlineColor for the color.
  121.  
  122.     Arguments:
  123.     - x, y, width, height - the bounding box of the rectangle
  124.     - thickness - the thickness of the border
  125.     '''
  126.     
  127.     def __init__(self, x, y, width, height, thickness):
  128.         self.thickness = thickness
  129.         Line.__init__(self, x, y, width, height)
  130.         self.connect('size-allocate', self._on_size_allocate)
  131.  
  132.     
  133.     def _on_size_allocate(self, win, allocation):
  134.         '''Sets the shape of the window to a hollow rectangle.'''
  135.         width = allocation.width
  136.         height = allocation.height
  137.         bitmap = gtk.gdk.Pixmap(None, width, height, 1)
  138.         cr = bitmap.cairo_create()
  139.         cr.set_source_rgb(0, 0, 0)
  140.         cr.set_operator(cairo.OPERATOR_DEST_OUT)
  141.         cr.paint()
  142.         cr.set_line_width(self.thickness)
  143.         cr.set_operator(cairo.OPERATOR_OVER)
  144.         offset = self.thickness / 2
  145.         cr.rectangle(0 + offset, 0 + offset, width - self.thickness, height - self.thickness)
  146.         cr.stroke()
  147.         win.shape_combine_mask(bitmap, 0, 0)
  148.  
  149.  
  150.  
  151. def reset():
  152.     '''Destroys all windows we have put on the screen.'''
  153.     global _outlineWindows
  154.     for window in _outlineWindows:
  155.         window.destroy()
  156.     
  157.     _outlineWindows = []
  158.  
  159.  
  160. def erase():
  161.     '''Erases all windows we have put on the screen.'''
  162.     for window in _outlineWindows:
  163.         window.hide()
  164.     
  165.  
  166.  
  167. def draw(x, y, width, height):
  168.     '''Draws an outline for the given rectangle.  This might
  169.     be composed of multiple windows depending upon the
  170.     settings.outlineStyle.'''
  171.     if settings.outlineStyle == settings.OUTLINE_NONE:
  172.         pass
  173.     elif settings.outlineStyle == settings.OUTLINE_LINE:
  174.         y = y + height + settings.outlineMargin
  175.         height = settings.outlineThickness
  176.         (x, y, width, height) = _adjustToScreen(x, y, width, height)
  177.         if not _outlineWindows:
  178.             line = Line(x, y, width, height)
  179.             _outlineWindows.append(line)
  180.         else:
  181.             _outlineWindows[0].resize(width, height)
  182.             _outlineWindows[0].move(x, y)
  183.     elif settings.outlineStyle == settings.OUTLINE_BOX:
  184.         extra = settings.outlineThickness + settings.outlineMargin
  185.         x = x - extra
  186.         y = y - extra
  187.         width = width + 2 * extra
  188.         height = height + 2 * extra
  189.         (x, y, width, height) = _adjustToScreen(x, y, width, height)
  190.         if not _outlineWindows:
  191.             box = Box(x, y, width, height, settings.outlineThickness)
  192.             _outlineWindows.append(box)
  193.         else:
  194.             _outlineWindows[0].resize(width, height)
  195.             _outlineWindows[0].move(x, y)
  196.     elif settings.outlineStyle == settings.OUTLINE_WEDGES:
  197.         wedgeSize = 4 * settings.outlineThickness
  198.         if width < wedgeSize:
  199.             diff = wedgeSize - width
  200.             width = wedgeSize
  201.             x -= diff / 2
  202.         
  203.         topy = y - 4 * settings.outlineThickness
  204.         bottomy = y + height + settings.outlineMargin
  205.         (topx, topy, topwidth, topheight) = _adjustToScreen(x, topy, width, wedgeSize)
  206.         (bottomx, bottomy, bottomwidth, bottomheight) = _adjustToScreen(x, bottomy, width, wedgeSize)
  207.         if not _outlineWindows:
  208.             top = Wedge(topx, topy, topwidth, topheight, True)
  209.             _outlineWindows.append(top)
  210.             bottom = Wedge(bottomx, bottomy, bottomwidth, bottomheight, False)
  211.             _outlineWindows.append(bottom)
  212.         else:
  213.             _outlineWindows[0].resize(topwidth, topheight)
  214.             _outlineWindows[0].move(topx, topy)
  215.             _outlineWindows[1].resize(bottomwidth, bottomheight)
  216.             _outlineWindows[1].move(bottomx, bottomy)
  217.     
  218.     for window in _outlineWindows:
  219.         window.present()
  220.         
  221.         try:
  222.             window.window.set_user_time(orca_state.lastInputEventTimestamp)
  223.         continue
  224.         continue
  225.  
  226.     
  227.  
  228.